package com.ywy.controller;

import com.ywy.entity.Comment;
import com.ywy.entity.Res;
import com.ywy.entity.User;
import com.ywy.service.CommentService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 评论Controller
 *
 * @author ywy
 * @date 2022/9/8 14:12
 */
@Controller
@RequestMapping("comment")
public class CommentController {
    @Resource
    private CommentService commentService;

    /**
     * 分页查询资源的评论信息
     * @param comment
     * @param page
     * @return
     */
    @ResponseBody
    @RequestMapping(value="list")
    public List<Comment> list(Comment comment, Integer page) {
        comment.setStatus(1); // 只显示审核通过的
        return commentService.list(comment, page, 6);
    }

    /**
     * 保存评论信息
     *
     * @param comment
     * @param session
     * @return
     * @throws Exception
     */
    @ResponseBody
    @PostMapping("save")
    public Map<String, Object> save(Comment comment, HttpSession session) throws Exception {
        comment.setCreateDate(new Date());
        comment.setStatus(0);
        comment.setUser((User) session.getAttribute("currentUser"));
        commentService.save(comment);

        Map<String, Object> resultMap = new HashMap<String, Object>();
        resultMap.put("success", true);
        return resultMap;
    }

    /**
     * 分页查询资源的评论信息
     *
     * @param comment
     * @param page
     * @param limit
     * @param session
     * @return
     */
    @ResponseBody
    @RequestMapping("userCommentList")
    public Map<String, Object> userCommentList(Comment comment, Integer page, Integer limit, HttpSession session) {
        // 查询用户资源信息
        User user = (User) session.getAttribute("currentUser");
        Res res = new Res();
        res.setUser(user);
        comment.setRes(res);
        comment.setStatus(1);
        List<Comment> resList = commentService.list(comment, page, limit);
        Long count = commentService.getTotal(comment);

        Map<String, Object> resultMap = new HashMap<>();
        resultMap.put("code", 0);
        resultMap.put("count", count);
        resultMap.put("data", resList);
        return resultMap;
    }

    /**
     * 根据id删除评论
     *
     * @param id
     * @return
     */
    @ResponseBody
    @RequestMapping("delete")
    public Map<String, Object> delete(Integer id) {
        commentService.delete(id);

        Map<String, Object> resultMap = new HashMap<>();
        resultMap.put("success", true);
        return resultMap;
    }

    /**
     * 批量删除
     *
     * @param ids
     * @return
     */
    @ResponseBody
    @RequestMapping("deleteBatch")
    public Map<String, Object> deleteBatch(String ids) {
        String[] idsStr = ids.split(",");
        for (int i = 0; i < idsStr.length; i++) {
            commentService.delete(Integer.parseInt(idsStr[i]));
        }

        Map<String, Object> resultMap = new HashMap<>();
        resultMap.put("success", true);
        return resultMap;
    }
}
